package com.ljy.my_study.commons;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class B {
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public static void main(String[] args) throws Exception {
		ObjectMapper om=new ObjectMapper();
		Person person=new Person();
		person.setName("james");
		person.setAge(22);
		person.setSex("男");
		person.setStature(170.0);
		String[] classes=new String[] {"数学","语文","英语"};
		
		String[] ss=om.readValue(om.writeValueAsString(classes), String[].class);
		System.out.println(ss);
		
		Clothing clothing=new Clothing();
		clothing.setColor("blue");
		clothing.setKind("T-shut");
		clothing.setSex("男");
		clothing.setSize("x");
		person.setClothes(Arrays.asList(new Clothing[]{clothing}));
		
		person.setClassList(Arrays.asList(classes));
		String personStr=om.writeValueAsString(person);
		System.out.println(personStr);
//		Person next=om.readValue(personStr, Person.class);
		Person next=om.readValue(personStr, new TypeReference<Person>() { });
		
		
		List<String> classess=next.getClassList();
		System.out.println(classess);
//		String[] classess=om.readValue(next.getClassList(), String[].class);
		System.out.println(next);
		List<Person> persons=new ArrayList<>();
		for(int i=0;i<3;i++) {
			persons.add(person);
		}
		String personsStr=om.writeValueAsString(persons);
		System.out.println(personsStr);
		
//		Person[] personArr=om.readValue(personsStr, Person[].class);
		JsonNode personArr=om.readValue(personsStr, JsonNode.class);
		System.out.println(personArr);
//		for(Person p:personArr) {
//			System.out.println(p);
//		}
		JsonNode personsjNode=om.readTree(personsStr);
		JsonNode personjNode=om.readTree(personStr);
		System.out.println(personsjNode.isArray());
		if(personsjNode.isArray()) {
			Iterator<JsonNode> jNIt=personsjNode.iterator();
			while(jNIt.hasNext()) {
				JsonNode sonNode=jNIt.next();
				Person one=om.readValue(sonNode.toString(), Person.class);
				System.out.println(one);
				System.out.println(one.getClassList());
			}
		}
	}
}
